package com.cqut.forum.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.User;
import com.cqut.forum.entity.FaceWall;
import com.cqut.forum.service.WallService;

@Controller
public class WallAction {
	@Resource
	private WallService wallService;
	
	@RequestMapping("/wall/create_wall")
	@ResponseBody
	public String createWall(HttpServletRequest request,int left,int top,int width,
			String frameColor,String pictureColor,String pictureSrc,String wallName,
			String wallContent,int pictureRotate){
		User user = (User)request.getSession().getAttribute("user");
		FaceWall wall = new FaceWall();
		wall.setFrameColor(frameColor);
		wall.setWallLeft(left);
		wall.setPictureColor(pictureColor);
		String src = pictureSrc.substring(pictureSrc.indexOf("user"), pictureSrc.length());
		wall.setPictureSrc("../../../"+src);
		wall.setPublishTime(new Date());
		wall.setWallRotate(pictureRotate);
		wall.setWallTop(top);
		wall.setUserId(user.getUserId());
		wall.setWallName(wallName);
		wall.setWallWidth(width);
		wall.setWallContents(wallContent);
		wall.setWallState(true);
		
		wall = wallService.createWall(wall);
		
		return "success";
	}
	
	@RequestMapping("/wall/get_wall")
	@ResponseBody
	public List<Map<String, Object>> getWall(int index,int size,String[] option){
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		String opt = "";
		//for(int i=0;i<option.length;i++){
		//	opt = opt + " wall_Name like \"%"+option[i]+"%\"";
		//}
		if(option.length>0){
			opt = opt + " and wall_name like \"%"+option[0]+"%\"";
		}
		if(option.length>1){
			opt = opt + " and publish_time>='"+option[1]+"' ";
		}
		
		List<Map<String, Object>> walls = wallService.getWallByOption(index,size,opt);
		for(Map<String, Object> m:walls){
			Map<String, Object>	 map = new HashMap<String, Object>();
			map.put("pictureSrc", m.get("picture_src"));
			map.put("wallName", m.get("wall_name"));
			map.put("wallLeft", m.get("wall_left"));
			map.put("wallTop", m.get("wall_top"));
			map.put("wallWidth", m.get("wall_width"));
			map.put("frameColor", m.get("frame_color"));
			map.put("pictureColor", m.get("picture_color"));
			map.put("wallId", m.get("wall_id"));
			map.put("wallContent", m.get("wall_contents"));
			map.put("wallRotate", m.get("wall_rotate"));
			
			result.add(map);
		}
		
		return result;
	}
	
	@RequestMapping("/control/get_wall")
	@ResponseBody
	public List<Map<String, Object>> getWall(HttpServletRequest request,int index,int size){
		User user = (User)request.getSession().getAttribute("user");
		List<Map<String, Object>> walls = wallService.getWall(user.getUserId(),index,size);
		return walls;
	}
	
	@RequestMapping("/wall/count_wall")
	@ResponseBody
	public int countWall(HttpServletRequest request){
		User user = (User)request.getSession().getAttribute("user");
		int result = wallService.countWall(user.getUserId());
		return result;
	}
	
	@RequestMapping("/wall/delete_wall")
	@ResponseBody
	public String deteWall(int wallId){
		FaceWall result = wallService.delete(wallId);
		if(result==null){
			return "false";
		}
		return "true";
	}
}
